package cn.u313.service;

import cn.u313.comm.conststring.Const;
import cn.u313.comm.conststring.RobberyString;
import cn.u313.comm.conststring.粽子;
import cn.u313.comm.eunm.Type;
import cn.u313.comm.utils.*;
import cn.u313.config.BotConfig;
import cn.u313.entry.*;
import cn.u313.model.ExecuteModel;
import cn.u313.repository.*;
import com.google.gson.Gson;
//import com.tencentcloudapi.ckafka.v20190819.models.User;
import me.kuku.utils.DateTimeFormatterUtils;
import me.kuku.utils.MyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import static cn.u313.comm.conststring.Const.yu;
import static cn.u313.comm.conststring.MoYuTxt.yuName;
import static cn.u313.comm.conststring.MoYuTxt.yuOk;
import static cn.u313.comm.conststring.PresentEunm.手动下单类;
import static cn.u313.comm.conststring.PresentEunm.未开始;
import static cn.u313.comm.eunm.Type.类型_悦币;
import static cn.u313.comm.utils.BiaoQing.getImg;
import static cn.u313.comm.utils.Util.getTime;
import static cn.u313.config.BotConfig.hongBaos;
import static cn.u313.listener.BaseListen.notManager;

@Service
public class UserEntryService {
    private String DAJIE = "dajie";
    private String  preDaJieQQ = "";
    private int max = 5;
    private int maxHongBao = 10;
    private int p = 19;
    String time = "07:00";
    @Autowired
    UserEntryRepository userEntryRepository;
    @Autowired
    JiLuEntryRepository jiLuEntryRepository;
    @Autowired
    HongBaoJiLuEntryRepository hongBaoJiLuEntryRepository;
    @Autowired
    UserLingQuEntryRepository userLingQuEntryRepository;
//    @Autowired
//    RedisUtils redisUtils;
    @Autowired
    PresentRecordRepository presentRecordRepository;
    //@Autowired

    @Autowired
    DragonBoatFestivalRepository dragonBoatFestivalRepository;
    /**
     * code=313185370
     * password=anhao445
     * @param qq
     * @return
     */


    private String getDAJIE(String qq){
        return DAJIE + qq;
    }
    /**
     * 注册
     * @param qq
     * @param group
     * @return
     */
    public UserEntry regsiger(String qq,String group){
        UserEntry userEntry = new UserEntry();
        userEntry.setM(100);
        userEntry.setQq(qq + "");
        userEntry.setQiandao(0);
        userEntry.setQun(group);
        userEntry.setGptToken(1000);
        return userEntryRepository.save(userEntry);
    }
    public UserEntry getUser(String qq){
        return userEntryRepository.byQQ(qq);
    }
    /**
     * 签到
     * @param qq
     * @param group
     * @return
     */
    public String qianDao(String qq,String group) {
        UserEntry userEntry0 = userEntryRepository.byQQ(qq);
        if(userEntry0 == null){
            userEntry0 = regsiger(qq,group);
            if(userEntry0==null){
                return XmlUtil.getXml("注册失败","注册失败！\n请稍后再试试看！");
            }
        }
        int q = 20 + (int) (Math.random() * 10);

        UserEntry userEntry = userEntryRepository.byQQ(qq);
        int a = new Integer(Util.getDate());
        //是否已签到
        if(userEntry.getQiandao() == a)
            return XmlUtil.getXml("签到失败","签到失败！\n你今天已经签到过了哦！");
        String date = DateTimeFormatterUtils.formatNow("yyyy-MM-dd ")+ time;
        int numberQiandao = jiLuEntryRepository.getByTimeNumber(Type.签到, date);
        System.out.println("numberQiandao>>>>"+numberQiandao +date);
        boolean flag = false;
        /**
         * 有活动 不计前几打卡奖励
         */
        if(Util.getTime(0) >=20221001&&Util.getTime(0)<=20221007){
            q = 66;
        }else {

            if(numberQiandao < max&&
                    new Integer(DateTimeFormatterUtils.formatNow("HHmm"))>=700){
                q = 40-numberQiandao ;
                flag = true;
            }
        }
        if(userEntry.payMe(q)){
            userEntry.setQiandao(a);
            JiLuEntry jiLuEntry = new JiLuEntry();
            jiLuEntry.setN(q);
            jiLuEntry.setQ0n(userEntry.getM());
            jiLuEntry.setQq0(qq);
            jiLuEntry.setTime(getTime());
            jiLuEntry.setType(Type.签到);
            jiLuEntry.setGroupNumber(group);
            jiLuEntryRepository.save(jiLuEntry);
            userEntryRepository.save(userEntry);



            /**
             * 粽子活动
             */
            boolean zongz = false;
            String zongName = "";


            if(flag){
                return XmlUtil.getXml("早上好鸭，你是 "+(numberQiandao+1)+" 位签到!","签到成功！\n7点开始的第"+(numberQiandao+1)+"位哦！\n获得" + q  +" 悦币!");
            }
//            if(){}
            return XmlUtil.getXml("签到成功","获得" + q  +" 悦币!"+(zongz?"获得一个 "+zongName+" ":""));
        }else{
            //悦币不足
        }

        return XmlUtil.getXml("签到失败","签到失败，\n请稍后再试试看！");
    }

    /**
     * 打劫功能
     * @param qq
     * @param qq1
     * @param group
     * @return
     */
    public String daJie(String qq, String qq1, String group) {
        if(qq.equals(qq1))
            return "自己打劫自己？";
        if(preDaJieQQ.equals(qq))
            return "不能连续打劫哦！稍微歇歇吧";
        if(!StringUtils.hasLength(qq1))
            return "没有选择需要抢劫的对象哦!";
        UserEntry u0 = userEntryRepository.byQQ(qq);//操作者
        UserEntry u1 = userEntryRepository.byQQ(qq1);//被操作者
        if(u0==null)return "请先发送【签到】进行注册";
        if(u1==null)return "没有选择需要抢劫的对象哦！";
        if(jiLuEntryRepository.countQqToQq1(qq,qq1,Type.打劫)!=0)
            return "今天已经洗劫过ta啦，\n明天再试试吧！";


        int frequency = jiLuEntryRepository.countByqqAndType(qq, Type.打劫);

        if(BotConfig.DaJieCiShu < frequency)
            return "今天已经劫了"+BotConfig.DaJieCiShu+"次啦，\n明天再来了吧";

        int num=(int)(Math.random()*((Math.sqrt(u1.getM()/30)+5)*100/200)+0.5) - (frequency)/2;
        /**
         * 粽子活动
         */
        boolean zongz = false;
        String zongName = "";
        if (!notManager(qq)&&Util.randomBoolAnd端午()){
            zongz = true;
            DragonBoatFestivalEntry dragonBoatFestivalEntry = new DragonBoatFestivalEntry();
            dragonBoatFestivalEntry.setAddDate(Util.getTime());
            dragonBoatFestivalEntry.setId(MyUtils.randomNT(3));
            dragonBoatFestivalEntry.setQq(qq);
            dragonBoatFestivalEntry.setZong(粽子.toZongZir());
            dragonBoatFestivalEntry.setHasInvalid(0);
            dragonBoatFestivalEntry.setRefZong(粽子.包粽子);
            dragonBoatFestivalEntry.setUGroup(group);

            zongName = dragonBoatFestivalEntry.getZong();

            /**
             * 记录操作
             */
            JiLuEntry y = new JiLuEntry();

            y.setQ0n(0);
            y.setQ1n(0);
            y.setQq0(qq);
            y.setQq0(qq1);
            y.setTime(getTime());
            y.setType(粽子.打劫赠送);
            y.setMsg(dragonBoatFestivalEntry.getZong());
            y.setGroupNumber(group);

            jiLuEntryRepository.save(y);
            dragonBoatFestivalRepository.save(dragonBoatFestivalEntry);

        }
/////////////////////////////


//        else if(num<0)
//            num = 2;
//        int num = 1;
        double r=Math.random();

        JiLuEntry y = new JiLuEntry();

        y.setQ0n(u0.getM());
        y.setQ1n(u1.getM());
        y.setQq0(qq);
        y.setQq1(qq1);
        y.setTime(getTime());
        y.setType(Type.打劫);
        y.setGroupNumber(group);

        int nuv = u0.getM() - u1.getM();
        nuv = nuv>0?nuv:-nuv;
        if( nuv > 8000 ){
            num = num/5;
        }else if( nuv > 3000 ){
            num = num/3;
        }else if(u0.getM()<500){
            num = 1;
        }
        if(r*100>6*frequency+40){
            num = num==0?1:num;
            if(Math.random()*10<6){
                y.setN(num);
            }else{
                y.setN(num>10?-num/3:-num);
            }
            u0.payMe(y.getN());
            u1.payMe(-y.getN());
            userEntryRepository.save(u0);
            userEntryRepository.save(u1);
        }else
            y.setN(0);


        preDaJieQQ = qq;
        jiLuEntryRepository.save(y);
        if(y.getN() == 0)
            return TxtUtil.radomTxt(RobberyString.unknownMsgs)+(zongz?"\n获得一个 "+zongName:"");
        else if(y.getN() < 0)
            return TxtUtil.radomTxtToF(RobberyString.failMsgs,-y.getN())+"当前悦币数量："+u0.getM()+"\n当前红包余额："+u0.getHongBao()+(zongz?"\n获得一个 "+zongName:"");

        return TxtUtil.radomTxtToF(RobberyString.successMsgs,y.getN())+"当前悦币数量："+u0.getM()+"\n当前红包余额："+u0.getHongBao()+(zongz?"\n获得一个 "+zongName:"");
    }

    /**
     * 抽红包
     * @param qq
     * @param group
     * @param n
     * @param hongBaos
     * @return
     */
    public String chouHongBao(String qq, String group, int n, double[] hongBaos) {
//
        boolean q = false;
        if(Util.getTime(0) >=20221001&&Util.getTime(0)<=20221007){

            q = (int) (Math.random() * 100 % 10) > 3;//概率;
        }else
            q = (int) (Math.random() * 100 % 10) > 6;//概率;
        int number = jiLuEntryRepository.countByqqAndType(qq,Type.抽红包);
        if(number>=maxHongBao){
            return "每天只能抽 "+max+ " 次红包哦！";
        }
        UserEntry u0 = getUser(qq);

        if (u0 == null ) {
            return ( "请先签到获取悦币");
        } else {
            double ns = 0;
            if (q) {
//                boolean z = (int) (Math.random() * 100) > 60;
//                if (z) {
//                    //
//                    ns = hongBaos[(int) (Math.random() * (hongBaos.length ))];
//                } else {
//                    ns = hongBaos[(int) (Math.random() * hongBaos.length)];
//                }
                ns = hongBaos[(int) (Math.random() * hongBaos.length)];
            }
            u0.setM(u0.getM() - n);
            u0.setHongBao(u0.getHongBao() + ns);
            if (u0.getM() > 0) {
                if (u0.getQun() == null || !u0.getQun().contains(group))
                    u0.setQun(u0.getQun() == null ? "" : group + "|" + u0.getQun());
                HongBaoJiLuEntry hongBaoJiLu = new HongBaoJiLuEntry();
                hongBaoJiLu.setN(Type.抽红包);
                hongBaoJiLu.setGuid(UUID.randomUUID().toString());
                hongBaoJiLu.setHongBao(ns);
                hongBaoJiLu.setQq(qq);
                hongBaoJiLu.setQqGroup(group);
                hongBaoJiLu.setTime(Util.getDate());
                hongBaoJiLuEntryRepository.save(hongBaoJiLu);


                JiLuEntry jiLuEntry = new JiLuEntry();
                jiLuEntry.setN(n);
                jiLuEntry.setQ0n(u0.getM());
                jiLuEntry.setQq0(qq);
                jiLuEntry.setTime(getTime());
                jiLuEntry.setType(Type.抽红包);
                jiLuEntry.setGroupNumber(group);
                jiLuEntryRepository.save(jiLuEntry);
                userEntryRepository.save(u0);

                return ((ns == 0 ? "诶嘿嘿没抽中哦！" : "恭喜你抽中一份红包 \n获得 " + ns + "元") + "\n本次抽奖扣除"+n+"悦币\n你的红包余额：" + u0.getHongBao() + "\n当前悦币数量: " + u0.getM() + getImg());
                //return ;
            } else {
                return ( "悦币不足" + getImg());
            }



        }
    }

    /**
     *
     * @param qq
     * @param group
     * @return
     */
    public String paiHang(String qq, String group) {
        if (group == null) {
            return "获取异常【group null】";
        }
        List<UserEntry> u = userEntryRepository.getPaiXu10("%"+group+"%");
        if (u == null || u.size() == 0) {
            return ("没有获取到信息");
        } else {
            String str = "悦币数量排名前10\n";
            for (int i = 0; i < u.size(); i++) {
                UserEntry u0 = u.get(i);
                str += (i + 1) + ".【" + u0.getQq() + "】  (" + u0.getM() + "悦币)\n";
//                msgSender.GETTER.getStrangerInfo().getNickname()
            }
            return str;
        }


    }

    public String getLingQu(String qq, String group) {
        List<UserLingQuEntry> ues= userLingQuEntryRepository.byQQ(qq);
        if(ues==null||ues.size()==0){
            return "您暂时没有可领取的奖励哦";
        }else{
            String msgs="您可领取:\n";
            for(UserLingQuEntry u0:ues){
                msgs+=u0.toString(0);
            }
            msgs+="发送：领取奖励【序号】领取";
            return msgs;
        }

    }
    @Transactional
    public String lingQu(String qq, int n, String group) {
        UserEntry userEntry = userEntryRepository.byQQ(qq);
        if (userEntry == null) {
            return "请先发送签到进行注册哦！";
        }
        UserLingQuEntry userLingQuEntry= userLingQuEntryRepository.byId(""+n);
        if (userLingQuEntry == null||!userLingQuEntry.getQq().equals(userEntry.getQq())) {
            return "没有找到该礼包哦！【"+n+"】";
        }

        userLingQuEntry.setState(Type.已领取);
        if(userLingQuEntry.getType()==类型_悦币)
            userEntry.payMe((int)(userLingQuEntry.getN()));
        JiLuEntry jiLuEntry = new JiLuEntry();
        jiLuEntry.setN((int)userLingQuEntry.getN());
        jiLuEntry.setQ0n(userEntry.getM());
        jiLuEntry.setQq0(qq);
        jiLuEntry.setTime(getTime());
        jiLuEntry.setType(Type.领取礼包);
        jiLuEntry.setGroupNumber(group);
        jiLuEntryRepository.save(jiLuEntry);
        userEntryRepository.save(userEntry);
        userLingQuEntryRepository.save(userLingQuEntry);
        return userLingQuEntry.ok();
    }
    int zsNumber = 3;
    public String zengsong(String qq, String qq1, int n, String group) {

        if (qq1 == null || n == 0) {
            return  "说清楚一点嘛! 送给谁，送多少";
        }
        if (!qq.equals("1054980699") && qq.equals(qq1)) {
            return "不能自己送自己哦";
        }
        //赠送过
        if (jiLuEntryRepository.countByqqAndType(qq,Type.赠送) > zsNumber) {
            if (!qq.equals("1054980699") ) {
                return "赠送失败,今日赠送次数已达上限 【"+zsNumber+"】" ;
            }
        }
        int aaqtqq = jiLuEntryRepository.countQqToQq1andType(qq,qq1,Type.赠送);
        if(aaqtqq>0&&!qq.equals("1054980699") )
            return "每天只能给他赠送一次哦！";
        if(n<=0){
            return "请输入合法的数量哦！";
        }

        try {
            n = n < 0 ? -n : n;
            if (!qq.equals("1054980699")  && n > 10) {
                return "赠送失败,赠送数量不能大于 10 悦币 " ;
            }

            UserEntry userEntry0 = userEntryRepository.byQQ(qq);
            UserEntry userEntry1 = userEntryRepository.byQQ(qq1);
            if (userEntry0 == null)
                return "请先发送签到获取悦币哦！";
            if (userEntry1 == null)
                return "请先让对方发送签到哦！";
            if(!userEntry0.payMe(-n))
                return "悦币数量不足【"+userEntry0.getM()+"】";
            userEntry1.payMe(n);
            JiLuEntry jiLuEntry = new JiLuEntry();
            jiLuEntry.setN(n);
            jiLuEntry.setQ0n(userEntry0.getM());
            jiLuEntry.setQ1n(userEntry1.getM());
            jiLuEntry.setQq0(qq);
            jiLuEntry.setQq1(qq1);
            jiLuEntry.setTime(getTime());
            jiLuEntry.setType(Type.赠送);
            jiLuEntry.setGroupNumber(group);
            jiLuEntryRepository.save(jiLuEntry);
            userEntryRepository.save(userEntry0);
            userEntryRepository.save(userEntry1);
            return "赠送成功，"+jiLuEntry.getQq1()+"获得 "+ jiLuEntry.getN() + " 悦币\n你当前悦币数 "+userEntry0.getM();
        } catch (Exception e) {
            return ("赠送失败" + "【"+e.getMessage()+"】");
        }
    }

    public String duiHuan(String qq, double n, String group) {
        UserEntry userEntry = userEntryRepository.byQQ(qq);
        if (userEntry == null) {
            return "请先发送签到进行注册哦！";
        }
        if(n<=0)
            return "请输入有效的数量!";
        if(userEntry.getHongBao() < n){
            return "兑换失败\n你的红包不足【"+n+"】";
        }
        userEntry.payMe((int)(n*1000));
        userEntry.setHongBao(userEntry.getHongBao()-n);
        JiLuEntry jiLuEntry = new JiLuEntry();
        jiLuEntry.setN((int)(n*1000));
        jiLuEntry.setQ0n(userEntry.getM());
        jiLuEntry.setQq0(qq);
        jiLuEntry.setTime(getTime());
        jiLuEntry.setType(Type.红包兑换悦币);
        jiLuEntry.setGroupNumber(group);
        jiLuEntryRepository.save(jiLuEntry);
        userEntryRepository.save(userEntry);
        return "已成功将【"+n+"元】\n兑换成【"+(int)(n*1000)+"悦币】\n当前悦币数量【"+userEntry.getM()+"】";

    }

    public String shiLianHongbao(String qq, double n, String group) {
        UserEntry userEntry = getUser(qq);
        if (userEntry == null) {
            return "请先发送签到进行注册哦！";
        }
        int yb =(int)(n*10*0.9);
        if(userEntry.getM()<yb)
            return "悦币不足!\n当前悦币数量【"+userEntry.getM()+"】\n十连需要的悦币数量【"+yb+"】";
        int i = 10;
        double add = 0;
        List<Double> ds = new ArrayList<>();
        List<Double> ds1 = new ArrayList<>();
        for (int i1 = 0; i1 < i; i1++) {

            double ns= 0;
            boolean q = (int) (Math.random() * 100 % 10) > 6;//概率;
            if (q) {
                boolean z = (int) (Math.random() * 100) > 80;
                if (z) {
                    //
                    ns += hongBaos[(int) (Math.random() * (hongBaos.length - 120) + 120)];
                } else {
                    ns += hongBaos[(int) (Math.random() * hongBaos.length)];
                }

            }
            if(i1 == 9)
                if(add<0.04)
                    ns=0.03;
            //加
            add+= ns;
            if(i1<5)
                ds.add(ns);
            else
                ds1.add(ns);

        }
        add = (int)(add*100)/100.0;
        return "前五次"+ds.toString()+"\n后五次"+ds1.toString()+"\n总计抽中："+add+"元\n消耗悦币："+yb+"\n当前悦币："+userEntry.getM()+"\n红包余额："+userEntry.getHongBao()+"\n该数据为测试数哦！";

    }

    public String shiLianYB(String qq, int n, String group) {
        UserEntry userEntry = getUser(qq);
        if (userEntry == null) {
            return "请先发送签到进行注册哦！";
        }
        int yb =(int)(n*10*0.9);
        if(userEntry.getM()<yb)
            return "悦币不足!\n当前悦币数量【"+userEntry.getM()+"】\n十连需要的悦币数量【"+yb+"】";
        int i = 10;
        Integer add = 0;
        List<Integer> ds = new ArrayList<>();
        List<Integer> ds1 = new ArrayList<>();
        for (int i1 = 0; i1 < i; i1++) {

            int ns= 0;
            boolean q = (int) (Math.random() * 100 % 10) > 2;//概率;
            if (q) {
                boolean z = (int) (Math.random() * 100) > 80;
                if (z) {
                    //
                    ns += hongBaos[(int) (Math.random() * (hongBaos.length - 120) + 120)]*1000;
                } else {
                    ns += hongBaos[(int) (Math.random() * hongBaos.length)]*1500;
                }

            }
            if(i1 == 9)
                if(add<0.04)
                    ns=(int)(0.03*1500);
            //加
            add+= ns;
            if(i1<5)
                ds.add(ns);
            else
                ds1.add(ns);

        }
        return "前五次"+ds.toString()+"\n后五次"+ds1.toString()+"\n总计抽中："+add+"悦币\n消耗悦币："+yb+"\n当前悦币："+userEntry.getM()+"\n该数据为测试数哦！";


    }

    public String moYu(String qq, String group) {
        String msgs = "";
//        if (Config.TYPE_NAME != TypeName.MO_YU) {
//            return;
//        }
        int n = (int) (Math.random() * 3) + 1;
//        int n =0;

        if (Const.fish == 0) {
//            msgs = "哎呀呀下次早点来嘛，本轮结束了哦 ,等下一个鱼群吧，\n你现在可以 去摸管理员试试，指不定...";
            return yuName[(int) (yuName.length * (Math.random()))] + "，本轮鱼都溜啦，下次早点哦！";
        } else {
            if (yu.containsKey(qq)) {
                return  "本轮 摸过\uD83D\uDC20了哦，你可以 去摸管理员试试，指不定...";
            } else if (Math.random() * 100 <50) {

                yu.put(qq, 1);
                Const.fish--;
                UserEntry u = getUser(qq);
                if (u == null ) {
                    return  "没有查到你的用户信息哦，请发送 ‘签到’ 进行注册";
                } else {
                    u.payMe(n);

                    JiLuEntry jiLuEntry = new JiLuEntry();
                    jiLuEntry.setN(n);

                    jiLuEntry.setQ0n(u.getM()-n);
                    jiLuEntry.setQ1n(u.getM());
                    jiLuEntry.setQq0(qq);
                    jiLuEntry.setTime(getTime());
                    jiLuEntry.setType(Type.摸鱼);
                    jiLuEntry.setGroupNumber(group);
                    jiLuEntryRepository.save(jiLuEntry);
                    userEntryRepository.save(u);
                    return yuOk[(int) (yuOk.length * (Math.random()))] + "，奖励你 " + n + " 悦币吧 ,目前还剩 " + Const.fish + " 条鱼! ";

                }
            } else {

                return  yuName[(int) (yuName.length * (Math.random()))] + "，这个东西不值悦币哦！";
            }
        }
    }
    public String sendJiangLi(String msg,String group){
        Gson gson =new Gson();
        try {

            ExecuteModel model = gson.fromJson(msg, ExecuteModel.class);
            if(model==null){
                return "ExecuteModel 为空！";
            }
            if(!model.b())
                return "某些内容为空！";
            UserLingQuEntry userLingQuEntry= new UserLingQuEntry();
            userLingQuEntry.setState(Type.未领取);
            userLingQuEntry.setN(new Integer(model.getNum()));
            userLingQuEntry.setEndTime("");
            userLingQuEntry.setQq(model.getQq());
            userLingQuEntry.setQgroup(group);
            userLingQuEntry.setStratTime(getTime());
            userLingQuEntry.setMsg(model.getContext());
            userLingQuEntry.setType(类型_悦币);// model.getType()

            JiLuEntry jiLuEntry = new JiLuEntry();
            jiLuEntry.setN((int)userLingQuEntry.getN());

            jiLuEntry.setQ0n(0);
            jiLuEntry.setQ1n(0);
            jiLuEntry.setQq0(userLingQuEntry.getQq());
            jiLuEntry.setTime(getTime());
            jiLuEntry.setType(Type.发放礼包);
            jiLuEntry.setManagerQQ(model.getManagerQQ());
            jiLuEntry.setGroupNumber(group);
            jiLuEntryRepository.save(jiLuEntry);

            userLingQuEntryRepository.save(userLingQuEntry);
            return "发放成功！"+model.toString();

//            if(userEntry.getM()<new Integer(model.getNum())){
//                return "对方悦币不足！";
//             }



        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public String chouHy(String qq, String group, int yueb, double[] hongBaos) {
        int n = 20;
        boolean q = false;
        if(Util.getTime(0)>20220314)
            return "活动结束了哦";
        int num = presentRecordRepository.getByNameAndQQ("扣扣会员",qq);
        int numZong = presentRecordRepository.getByName("扣扣会员");
        if(numZong>17){
            return "奖励已经发完了哦！\n下次记得早点参与哦";
        }
        if (num !=0) {
            return "活动期间每人只允许获得一份奖励哦！";
        }
        q = (int) (Math.random() * 100 + 1) < 6;//概率;
        int number = jiLuEntryRepository.countByqqAndType(qq, Type.抽会员);
        if (number >= 5) {
            return "每天只能抽 5 次会员哦！";
        }

        UserEntry u0 = getUser(qq);

        if (u0 == null) {
            return ("请先签到获取悦币");
        } else {
            double ns = 0;

            u0.setM(u0.getM() - n);
            if (u0.getM() > 0) {
                if (u0.getQun() == null || !u0.getQun().contains(group))
                    u0.setQun(u0.getQun() == null ? "" : group + "|" + u0.getQun());


                JiLuEntry jiLuEntry = new JiLuEntry();
                jiLuEntry.setN(n);
                jiLuEntry.setQ0n(u0.getM());
                jiLuEntry.setQq0(qq);
                jiLuEntry.setMsg(q + "");
                jiLuEntry.setTime(getTime());
                jiLuEntry.setType(Type.抽会员);
                jiLuEntry.setGroupNumber(group);
                if (q) {

                    PresentRecordEntry recordEntry = new PresentRecordEntry();
                    recordEntry.setName("扣扣会员");
                    recordEntry.setPrice(0);
                    recordEntry.setQq(qq);
                    recordEntry.setType(未开始);
                    recordEntry.setMainId("qqsvip");
                    recordEntry.setTypePresent(手动下单类);
                    recordEntry.setDate(new Date());

                    presentRecordRepository.save(recordEntry);
                }
                jiLuEntryRepository.save(jiLuEntry);

                userEntryRepository.save(u0);

                return ((!q ? "[CAT:face,id=137]诶嘿嘿没抽中会员哦！" : "[CAT:face,id=137]恭喜你抽中一份扣扣会员\n[CAT:face,id=137]联系群主兑换哦") + "\n[CAT:face,id=137]本次抽奖扣除" + n + "悦币\n[CAT:face,id=137]当前悦币数量: " + u0.getM() + getImg());
                //return ;
            } else {
                return ("悦币不足" + getImg());
            }
        }
    }

    public UserEntry save(UserEntry user) {
        return userEntryRepository.save(user);
    }
}
